package org.gw.building.repositories;

import org.gw.building.model.CourseAttendance;
import org.gw.building.model.CourseInfo;
import org.gw.building.model.StuInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface CourseAttendanceRepository extends JpaRepository<CourseAttendance, String> {

	@Query("from CourseAttendance t  where (?1 is null or t.courseInfo.id=?1) and (?2 is null or t.courseInfo.name like ?2) and (?3 is null or t.stuInfo.user.name like ?3 or t.stuInfo.user.uid like ?3)"
			+ " and (?4 is null or t.stuInfo.user.department=?4) and (?5 is null or t.stuInfo.user.major=?5) and (?6 is null or t.stuInfo.classInfo.id=?6) and (?7 is null or t.status=?7)")
	Page<CourseAttendance> query(String course, String courseName, String stuName, String department, String major, String clazz, Integer status,
                                 Pageable pageable);

	@Query("select t.courseInfo.id,t.status from CourseAttendance t where t.courseInfo.id in ?1")
	List<Object[]> findByCourseInfoIdIn(List<String> courseId);

	List<CourseAttendance> findByCourseInfoIdInAndStuInfoUserUid(List<String> courseId, String stuInfo);

	CourseAttendance findOneByCourseInfoAndStuInfo(CourseInfo courseInfo, StuInfo stuInfo);

	@Query("from CourseAttendance c where c.stuInfo.term.id = ?1")
	List<CourseAttendance> findByTerm(String term);

}
